<?php
declare (strict_types = 1);

namespace app\command;

use app\common\model\GzhAccount;
use app\common\model\GzhTitle;
use app\opengzh\service\OpenPlatformService;
use think\console\Command;
use think\console\Input;

use think\console\Output;
use think\facade\Db;

class Cmdpub extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('Cmdpub')
            ->setDescription('the Cmdpub command');
    }

    protected function execute(Input $input, Output $output)
    {
        //每个公众号发表文章的单日限制次数
        $day_pub_limit = 3;
        //是否随机
        $is_random = true;

        //每次随机获取发表的公众号个数，每个发一条
        $gzh_account = mt_rand(3, 5);

        //每天七点之后才发送
        $hour = date('H', time());
        if(($hour<8)){
            return;
        }

        if(($hour>=(23-$day_pub_limit))){
            $is_random = false;
        }

        $accountModel = new GzhAccount();
        
        $today = date('Y-m-d',time());

        if($is_random){
            $sql = 'SELECT t1.id,t1.* FROM gzh_account AS t1 JOIN 
                (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM gzh_account WHERE day_caogao_count > 0 AND is_use = 2 AND last_pub_day !='.$today.')
                -(SELECT MIN(id) FROM gzh_account WHERE day_caogao_count > 0 AND is_use = 2 AND last_pub_day !='.$today.'))
                +(SELECT MIN(id) FROM gzh_account WHERE day_caogao_count > 0 AND is_use = 2 AND last_pub_day !='.$today.')) AS id) AS t2 
                WHERE t1.id >= t2.id AND t1.day_caogao_count > 0 AND t1.is_use = 2 AND t1.last_pub_day !='.$today.' ORDER BY t1.id LIMIT '.$gzh_account.';';
            $account_list = Db::connect('mysql')->query($sql);

        }else{
            $account_list = $accountModel->where('day_caogao_count','>',0)
                ->where('is_use',2)
                ->where('last_pub_day','<>',$today)
                ->order('id','asc')
                ->select()->toArray();
        }

        //判断有没有今天还没生发过文章的公众号
        if(sizeof($account_list)>0){
            //这里是今天没发过文章的公众号
            $titleModel = new GzhTitle();
            foreach ($account_list as $v){
                $accountModel->where('id',$v['id'])->save([
                   'last_pub_day'=>$today,
                   'day_pub_count'=>0,
                ]);
                $title_info = $titleModel->where('appid',$v['appid'])
                        ->where('is_caogao',2)
                        ->where('is_article',2)
                        ->where('is_pub',1)
                        ->order('id','asc')
                        ->findOrEmpty();
                if(!$title_info->isEmpty()){
                    $title_info = $title_info->toArray();
                    $opfService =  new OpenPlatformService();
                    $res = $opfService->freepublish($v['appid'],$title_info);
                    if($res['status']){
                        $accountModel->where('id',$v['id'])->inc('day_pub_count')->update();
                        $accountModel->where('id',$v['id'])->inc('pub_count')->update();
                        $accountModel->where('id',$v['id'])->save([
                           'pub_hour'=>$hour
                        ]);
                        var_dump('发布成功');
                    }
                }
            }
        }else{
            //这里是今天已经发过文章的公众号
            if($is_random){
                $sql = 'SELECT t1.id,t1.* FROM gzh_account AS t1 JOIN 
                (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM gzh_account WHERE day_caogao_count > 0 AND is_use = 2 AND last_pub_day ='.$today.' 
                AND day_pub_count <'.$day_pub_limit.' AND pub_hour !='.$hour.')
                -(SELECT MIN(id) FROM gzh_account WHERE day_caogao_count > 0 AND is_use = 2 AND last_pub_day ='.$today.' 
                AND day_pub_count <'.$day_pub_limit.' AND pub_hour !='.$hour.'))
                +(SELECT MIN(id) FROM gzh_account WHERE day_caogao_count > 0 AND is_use = 2 AND last_pub_day ='.$today.' 
                AND day_pub_count <'.$day_pub_limit.' AND pub_hour !='.$hour.')) AS id) AS t2 
                WHERE t1.id >= t2.id AND t1.day_caogao_count > 0 AND t1.is_use = 2 AND t1.last_pub_day ='.$today.' 
                AND t1.day_pub_count <'.$day_pub_limit.' AND t1.pub_hour !='.$hour.' ORDER BY t1.id LIMIT '.$gzh_account.' ;';
                $account_list = Db::connect('mysql')->query($sql);

            }else{
                $account_list = $accountModel->where('day_caogao_count','>',0)
                    ->where('is_use',2)
                    ->where('last_pub_day',$today)
                    ->where('day_pub_count','<',$day_pub_limit)
                    ->where('pub_hour','<>',$hour)
                    ->order('id','asc')
                    ->select()
                    ->toArray();
            }


            if(sizeof($account_list)>0){
                $titleModel = new GzhTitle();
                foreach ($account_list as $v){
                    $title_info = $titleModel->where('appid',$v['appid'])
                        ->where('is_caogao',2)
                        ->where('is_article',2)
                        ->where('is_pub',1)
                        ->order('id','asc')
                        ->findOrEmpty();
                    if(!$title_info->isEmpty()){
                        $title_info = $title_info->toArray();
                        $opfService =  new OpenPlatformService();
                        $res = $opfService->freepublish($v['appid'],$title_info);
                        if($res['status']){
                            $accountModel->where('id',$v['id'])->inc('day_pub_count')->update();
                            $accountModel->where('id',$v['id'])->inc('pub_count')->update();
                            $accountModel->where('id',$v['id'])->save([
                                'pub_hour'=>$hour
                            ]);
                            var_dump('发布成功');
                        }
                    }
                }
            }else{
                var_dump('当日发布的任务已完成');
            }
        }
    	$output->writeln('Cmdpub - over');
    }
}
